home *** CD-ROM | disk | FTP | other *** search
- Public Class ApplicationForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
- AddHandler Application.Idle, AddressOf Application_Idle
- AddHandler Application.ApplicationExit, AddressOf Application_ApplicationExit
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
-
- RemoveHandler Application.Idle, AddressOf Application_Idle
- RemoveHandler Application.ApplicationExit, AddressOf Application_ApplicationExit
-
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents btnShowProperties As System.Windows.Forms.Button
- Friend WithEvents txtResult As System.Windows.Forms.TextBox
- Friend WithEvents chkShowIdle As System.Windows.Forms.CheckBox
- Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
- Friend WithEvents btnEnd As System.Windows.Forms.Button
- Friend WithEvents btnThrow As System.Windows.Forms.Button
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.btnShowProperties = New System.Windows.Forms.Button()
- Me.txtResult = New System.Windows.Forms.TextBox()
- Me.btnEnd = New System.Windows.Forms.Button()
- Me.chkShowIdle = New System.Windows.Forms.CheckBox()
- Me.ProgressBar1 = New System.Windows.Forms.ProgressBar()
- Me.btnThrow = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'btnShowProperties
- '
- Me.btnShowProperties.Location = New System.Drawing.Point(16, 16)
- Me.btnShowProperties.Name = "btnShowProperties"
- Me.btnShowProperties.Size = New System.Drawing.Size(112, 40)
- Me.btnShowProperties.TabIndex = 0
- Me.btnShowProperties.Text = "Show Properties"
- '
- 'txtResult
- '
- Me.txtResult.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtResult.Location = New System.Drawing.Point(152, 16)
- Me.txtResult.Multiline = True
- Me.txtResult.Name = "txtResult"
- Me.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.txtResult.Size = New System.Drawing.Size(440, 240)
- Me.txtResult.TabIndex = 1
- Me.txtResult.Text = ""
- Me.txtResult.WordWrap = False
- '
- 'btnEnd
- '
- Me.btnEnd.Location = New System.Drawing.Point(16, 80)
- Me.btnEnd.Name = "btnEnd"
- Me.btnEnd.Size = New System.Drawing.Size(112, 40)
- Me.btnEnd.TabIndex = 0
- Me.btnEnd.Text = "End the application"
- '
- 'chkShowIdle
- '
- Me.chkShowIdle.Location = New System.Drawing.Point(8, 272)
- Me.chkShowIdle.Name = "chkShowIdle"
- Me.chkShowIdle.Size = New System.Drawing.Size(304, 16)
- Me.chkShowIdle.TabIndex = 3
- Me.chkShowIdle.Text = "Show Idle Events"
- '
- 'ProgressBar1
- '
- Me.ProgressBar1.Dock = System.Windows.Forms.DockStyle.Bottom
- Me.ProgressBar1.Location = New System.Drawing.Point(0, 293)
- Me.ProgressBar1.Name = "ProgressBar1"
- Me.ProgressBar1.Size = New System.Drawing.Size(608, 16)
- Me.ProgressBar1.TabIndex = 4
- '
- 'btnThrow
- '
- Me.btnThrow.Location = New System.Drawing.Point(16, 144)
- Me.btnThrow.Name = "btnThrow"
- Me.btnThrow.Size = New System.Drawing.Size(112, 40)
- Me.btnThrow.TabIndex = 0
- Me.btnThrow.Text = "Throw an exception"
- '
- 'ApplicationForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(608, 309)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnThrow, Me.btnEnd, Me.ProgressBar1, Me.txtResult, Me.btnShowProperties, Me.chkShowIdle})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "ApplicationForm"
- Me.Text = "The Application object"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- ' show Application object properties
-
- Private Sub btnShowProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowProperties.Click
- Dim msg As String = "AllowQuit = {1}{0}CommonAppDataPath = {2}{0}" _
- & "CommonPathDataRegistry = {3}{0}CompanyName = {4}{0}" _
- & "CurrentCulture = {5}{0}CurrentInputLanguage = {6}{0}" _
- & "ExecutablePath = {7}{0}LocalUserAppDataPath = {8}{0}" _
- & "ProductName = {9}{0}ProductVersion = {10}{0}" _
- & "SafeTopLevelCaptionFormat = {11}{0}StartupPath = {12}{0}" _
- & "UserAppDataPath = {13}{0}UserAppDataRegistry = {14}{0}"
-
- txtResult.Text = String.Format(msg, ControlChars.CrLf, Application.AllowQuit, Application.CommonAppDataPath, _
- Application.CommonAppDataRegistry, Application.CompanyName, _
- Application.CurrentCulture, Application.CurrentInputLanguage.Culture, _
- Application.ExecutablePath, Application.LocalUserAppDataPath, _
- Application.ProductName, Application.ProductVersion, _
- Application.SafeTopLevelCaptionFormat, Application.StartupPath, _
- Application.UserAppDataPath, Application.UserAppDataRegistry)
- End Sub
-
- ' exit the application
-
- Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
- Application.Exit()
- End Sub
-
- ' simulate an exception, that is then trapped
-
- Private Sub btnThrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThrow.Click
- Throw New ArgumentException("The parameter was invalid")
- End Sub
-
- ' this event fires when the UI becomes idle again
-
- Private Sub Application_Idle(ByVal sender As System.Object, ByVal e As System.EventArgs)
- If chkShowIdle.Checked Then
- ProgressBar1.Value = (ProgressBar1.Value Mod ProgressBar1.Maximum) + 1
- End If
- End Sub
-
- ' this event fires when the application is exiting
-
- Private Sub Application_ApplicationExit(ByVal sender As System.Object, ByVal e As System.EventArgs)
- MessageBox.Show("The application is about to exit", "ApplicationExit event", MessageBoxButtons.OK, MessageBoxIcon.Information)
- End Sub
- End Class
-
- ' To test the ThreadException event and global error handlers,
- ' Sub Main should be the Startup object.
-
- Module MainModule
-
- <STAThread()> _
- Sub Main()
- ' Add the event handler to to the event.
- AddHandler Application.ThreadException, AddressOf Application_ThreadException
- ' Run the application.
- Application.Run(New StartupForm())
- End Sub
-
- ' this event fires when an unhandled exception is thrown
-
- Sub Application_ThreadException(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)
- Try
- ' Display an informative message.
- Dim msg As String = String.Format("An error has occurred:{0}{0}{1}{0}{0}{2}", _
- ControlChars.Cr, e.Exception.Message, e.Exception.StackTrace)
- Dim result As DialogResult = _
- MessageBox.Show(msg, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)
- If result = DialogResult.Abort Then
- Application.Exit()
- End If
-
- Catch
- ' if the above message box couldn't be displayed because the Exception object
- ' wasn't available, try with a simpler msgbox, and then close the application anyway
- '
- Try
- MessageBox.Show("The application will be terminated", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Finally
- Application.Exit()
- End Try
- End Try
- End Sub
-
- End Module